package com.EAsystem.controller.admin;

import com.EAsystem.entity.User;
import com.EAsystem.service.UserService;
import com.EAsystem.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取当前用户信息
     */
    @GetMapping("/profile")
    @ResponseBody
    public Map<String, Object> getUserProfile(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            User currentUser = SessionUtils.getCurrentUser(request);
            if (currentUser == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                return result;
            }
            
            // 重新从数据库获取最新的用户信息
            User user = userService.findById(currentUser.getId());
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户不存在");
                return result;
            }

            user.setPassword(null);
            result.put("success", true);
            result.put("user", user);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取用户信息失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新个人信息
     */
    @PostMapping("/updateProfile")
    @ResponseBody
    public Map<String, Object> updateProfile(@RequestParam String realName,
                                           @RequestParam(required = false) String email,
                                           @RequestParam(required = false) String phone,
                                           HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            User currentUser = SessionUtils.getCurrentUser(request);
            if (currentUser == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                return result;
            }
            
            // 更新用户信息
            User user = userService.findById(currentUser.getId());
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户不存在");
                return result;
            }
            
            user.setRealName(realName);
            
            userService.update(user);
            
            // 更新session中的用户信息
            SessionUtils.setCurrentUser(request, user);
            
            result.put("success", true);
            result.put("message", "个人信息更新成功");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 修改密码
     */
    @PostMapping("/changePassword")
    @ResponseBody
    public Map<String, Object> changePassword(@RequestParam String oldPassword,
                                            @RequestParam String newPassword,
                                            @RequestParam String confirmPassword,
                                            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            User currentUser = SessionUtils.getCurrentUser(request);
            if (currentUser == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                return result;
            }
            
            // 验证新密码和确认密码是否一致
            if (!newPassword.equals(confirmPassword)) {
                result.put("success", false);
                result.put("message", "新密码和确认密码不一致");
                return result;
            }
            
            // 验证新密码长度
            if (newPassword.length() < 6) {
                result.put("success", false);
                result.put("message", "新密码长度不能少于6位");
                return result;
            }
            
            // 获取用户完整信息
            User user = userService.findById(currentUser.getId());
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户不存在");
                return result;
            }
            
            // 验证原密码
            if (!oldPassword.equals(user.getPassword())) {
                result.put("success", false);
                result.put("message", "原密码错误");
                return result;
            }
            
            // 更新密码
            user.setPassword(newPassword);
            userService.update(user);
            
            result.put("success", true);
            result.put("message", "密码修改成功");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "修改密码失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 用户登出
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        SessionUtils.logout(request);
        return "redirect:/";
    }
} 